package com.llsydn.beijingnews.fragment;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.llsydn.beijingnews.R;
import com.llsydn.beijingnews.activity.MainActivity;
import com.llsydn.beijingnews.base.BaseFragment;
import com.llsydn.beijingnews.domain.NewsBean;
import com.llsydn.beijingnews.pager.NewsPager;
import com.llsydn.beijingnews.util.DensityUtils;
import com.llsydn.beijingnews.util.LogUtil;

import java.util.List;

/**
 * @author lilinshen
 * @title 左侧菜单的fragment
 * @description 请填写相关描述
 * @date 2018/10/25 9:12
 */
public class LeftmenuFragment extends BaseFragment {
    private List<NewsBean.DataBean> data;
    private ListView listView;
    private LeftmenuAdpater adpater;
    public int prePosition;

    @Override
    public View initView() {
        LogUtil.e("左侧菜单视图被初始化了");
        listView = new ListView(context);

        listView.setPadding(0, DensityUtils.dip2px(context, 40), 0, 0);
        listView.setDividerHeight(0); // 设置分割线高度为0
        listView.setCacheColorHint(Color.TRANSPARENT);
        listView.setSelector(android.R.color.transparent); // 设置按下listview的item不变色

        listView.setOnItemClickListener((parent, view, position, id) -> {
            // 1.记录点击的位置，变成红色
            view.setTag(position);
            prePosition = position;
            adpater.notifyDataSetChanged(); // getcount->getview
            // 2.把左侧菜单关闭
            MainActivity mainActivity = (MainActivity) context;
            mainActivity.getSlidingMenu().toggle();
            // 3.切换到对应的详情页面：新闻，专题，图组
            swichPager(position);

        });
        return listView;
    }

    /**
     * 根据位置切换新闻中心详情页面
     *
     * @param position
     */
    private void swichPager(int position) {
        MainActivity mainActivity = (MainActivity) context;
        ContentFragment contentFragment = mainActivity.getContentFragment();
        NewsPager newsPager = contentFragment.getNewsPager();
        newsPager.swichPager(position);
    }

    @Override
    public void initData() {
        super.initData();
        LogUtil.e("左侧菜单数据被初始化了");

    }

    /**
     * 接收数据newspager传递过来的data集合
     *
     * @param data
     */
    public void setData(List<NewsBean.DataBean> data) {
        this.data = data;
        // 设置适配器
        adpater = new LeftmenuAdpater();
        listView.setAdapter(adpater);

        // 设置默认页面
        swichPager(prePosition);
    }

    /**
     * 自定义的适配器adapter
     */
    class LeftmenuAdpater extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) View.inflate(context, R.layout.item_left_menu, null);
            textView.setText(data.get(position).getTitle());
            // 设置颜色
            textView.setEnabled(position == prePosition);
            return textView;
        }
    }


}